package com.coocaa.internship.dao;

import com.coocaa.internship.pojo.UserCV;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description: 用户简历Dao层
 * @Author: zwh
 * @Date: 2019-07-26
 * @Time: 1:43
 */
@Mapper
@Component
public interface UserCvDao {
    String TABLE_NAME = "user_cv";
    String SELECT_FIELDS = "user_cv_id , user_cv_pdf , user_cv_status , user_id";
    String INSERT_FIELDS = "user_cv_pdf , user_cv_status , user_id";

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where user_id = #{userId}"})
    List<UserCV> selectUserCvByUserId(@Param("userId") Long userId);

    /**
     * 查询该用户下所有已经投递出的简历id
     *
     * @param userId
     * @return
     */
    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where user_id = #{userId} and user_cv_status > 0"})
    List<UserCV> selectDeliveredCvByUserId(@Param("userId") Long userId);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where user_cv_id = #{userCvId}"})
    UserCV selectUserCvById(@Param("userCvId") Long userCvId);

    @Insert({"insert into ", TABLE_NAME, " (", INSERT_FIELDS, ") values (#{userCvPdf} , #{userCvStatus} , #{userId})"})
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    Integer insertUserCv(UserCV userCV);

    @Delete({"delete from ", TABLE_NAME, "where user_cv_id = #{userCvId}"})
    Integer deleteUserCvByUserCvId(@Param("userCvId") Long userCvId);

    @Update({"update ", TABLE_NAME, " set user_cv_status = user_cv_status + 1 where user_cv_id = #{userCvId}"})
    Integer increaseUserCvStatusByCvId(@Param("userCvId") Long userCvId);

    @Update({"update ", TABLE_NAME, " set user_cv_status = user_cv_status - 1 where user_cv_id = #{userCvId}"})
    Integer decreaseUserCvStatusByCvId(@Param("userCvId") Long userCvId);

    @Select({"select", SELECT_FIELDS, "from", TABLE_NAME, "where user_id = #{userId} and user_cv_id = #{userCvId}"})
    UserCV selectUserCvByUserIdAndUserCvId(@Param("userId") Long userId, @Param("userCvId") Long userCvId);

}
